import Vue from 'vue'
import VueRouter from 'vue-router'
// 注册路由插件
Vue.use(VueRouter)

import home from '../views/Home.vue'
import index from '../views/index.vue'
import shopcar from '../views/shopcar.vue'
import goodslist from '../views/goodslist.vue'
import user from '../views/user.vue'
import myorder from '../views/myorder.vue'
import orderlist from '../views/orderlist.vue'
import goodDetails from '../views/goodDetails.vue'
import search from '../views/search.vue'
import searchresult from '../views/searchresult.vue'
import address from '../views/address.vue'
import addaddress from '../views/addaddress.vue'
import editaddress from '../views/editaddress.vue'
import login from '../views/login.vue'
import zhuce from '../views/zhuce.vue'
import store from '../store/index.js'


const router = new VueRouter({
    routes: [{
        path: '/',
        component: index,
        redirect: 'home',
        children: [
            {
                path: '/home',
                name: home,
                component: home,
                meta: {
                    name: 'home',
                    isMainPage: true,
                },
                children: [
                    {
                        path: "search",
                        component: search,
                        name: "search",
                        meta: {
                            title: "搜索",
                            // isShowNavBar: false,
                        },
                    }]
            },
            {
                path: '/shopcar',
                name: shopcar,
                component: shopcar,
                meta: {
                    name: 'shopcar',
                    isMainPage: true,
                    RequireAuth: true
                }
            },
            {
                path: '/user',
                name: user,
                component: user,
                meta: {
                    name: 'user',
                    isMainPage: true,
                    RequireAuth: true
                }
            }
        ]
    },
    {
        path: '/login',
        component: login,
        meta: {
            title: "登录"
        }
    },
    {
        path: '/zhuce',
        component: zhuce,
        meta: {
            title: "注册"
        }
    },
    {
        path: '/goodslist',
        component: goodslist,
        meta: {
            title: "商品列表"
        }
    },
    {
        path: '/goodDetails/:id',
        component: goodDetails,
        meta: {
            title: "商品详情"
        }
    },
    {
        path: '/myorder',
        component: myorder,
        meta: {
            title: "我的订单",
            // RequireAuth:true
        }
    },
    {
        path: '/orderlist/:order_id',
        component: orderlist,
        meta: {
            title: "订单详情",
            RequireAuth:true
        }
    },
    {
        path: '/searchresult/:keyword',
        component: searchresult,
        meta: {
            title: "搜索商品"
        }
    },
    {
        path: '/address',
        component: address,
        meta: {
            title: "收货地址"
        }
    },
    {
        path: '/addaddress',
        component: addaddress,
        meta: {
            title: "添加地址"
        }
    },
    {
        path: '/editaddress/:addressInfo',
        props:true,
        component: editaddress,
        meta: {
            title: "编辑地址"
        }
    },

    ]
})

NProgress.configure({
    showSpinner: false
});

// 直接在路由守卫router.beforeEach钩子中强制next会出现空白页的问题，是因为该路由dom还未加载，此问题的解决方法可以统一对报错信息进行处理
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

router.beforeEach((to, from, next) => {
    NProgress.start();
    // console.log(to);
    // console.log(from);
    // 判断所访问的路由是否需要权限
    if (to.meta.RequireAuth) {
        // 需要权限，判断token
        let token = store.state.token;
        if (token) {
            next();
        } else {
            // 获取之前的上一次的访问页面，登录成功，便于在回到之前的页面
            next({
                path: "/login?redirect=" + to.fullPath
            })
        }
    } else {
        // 不需要权限
        next();
    }

})
router.afterEach((to, from) => {
    NProgress.done();
})



export default router
